package org.hishenframework.core.web.message.taglib;

import java.io.IOException;

import javax.servlet.http.HttpSession;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.TagSupport;

import org.hishenframework.core.web.message.Message;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class HishenMessageTag extends TagSupport {
	
	private static final long serialVersionUID = 1L;
	
	private Logger logger = LoggerFactory.getLogger(HishenMessageTag.class);

	@Override
	public int doStartTag() throws JspException {
		
		HttpSession session = pageContext.getSession();		
		Message message = (Message) session.getAttribute("message");
		session.removeAttribute("message");
		if (message != null) {
			logger.debug("message is " + message.getText());
			JspWriter out = pageContext.getOut();
			try {
				//显示第一页
				out.write("<script type='text/javascript'>");
				out.write("$(function(){");
				out.write("$.sticky('" + message.getText() + "')");
				out.write("});");
				out.write("</script>");
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return SKIP_BODY;
	}

}
